package service

import (
	"gorm.io/gorm"
	"strconv"
	"time"
	"zg3/config"
	"zg3/internal/api/request"
	"zg3/internal/models/dao"
)

func Check(request *request.SendFriendRequest, userId int) string {
	if len(request.FriendId) > 5 {
		return "您一次性只能向5名好友发送好友申请"
	}
	for _, i := range request.FriendId {
		var friend *dao.Friend
		config.DB.Where("user_id=? and friend_id=?", userId, i).Limit(1).Find(&friend)
		if friend.ID != 0 {
			return "用户" + strconv.Itoa(i) + "已经是您的好友了"
		}
	}
	return ""
}

func SendFriendRequest(request *request.SendFriendRequest, userId int) {
	for _, i := range request.FriendId {
		Num := "friend_add_:userId" + strconv.Itoa(i) + ":friendId" + strconv.Itoa(userId)
		config.Rdb.Set(config.Ctx, Num, userId, time.Hour*24)
	}
}

func Yes(r *request.YesOrNo, userId int) string {
	tx := config.DB.Begin()
	Num := "friend_add_:userId" + strconv.Itoa(userId) + ":friendId" + strconv.Itoa(r.FriendId)
	get := config.Rdb.Get(config.Ctx, Num)
	friendId, _ := strconv.Atoi(get.Val())
	var userMy *dao.User
	var userFriend *dao.User
	config.DB.Where("id=?", userId).Limit(1).Find(&userMy)
	config.DB.Where("id=?", friendId).Limit(1).Find(&userFriend)
	friend1 := dao.Friend{
		UserId:           userId,
		FriendId:         friendId,
		FriendNickName:   userFriend.NickName,
		FriendHeardImage: userFriend.HeardImage,
	}
	err := tx.Create(&friend1).Error
	if err != nil {
		tx.Rollback()
		return "好友添加失败"
	}
	friend2 := dao.Friend{
		UserId:           friendId,
		FriendId:         userId,
		FriendNickName:   userMy.NickName,
		FriendHeardImage: userMy.HeardImage,
	}
	err = tx.Create(&friend2).Error
	if err != nil {
		tx.Rollback()
		return "好友添加失败"
	}
	tx.Commit()
	config.Rdb.Del(config.Ctx, Num)
	return ""
}

func No(r *request.YesOrNo, userId int) {
	Num := "friend_add_:userId" + strconv.Itoa(userId) + ":friendId" + strconv.Itoa(r.FriendId)
	config.Rdb.Del(config.Ctx, Num)
}

func FindFriend(friendId int, userId int) string {
	var friend *dao.Friend
	config.DB.Where("user_id=? and friend_id=?", userId, friendId).Limit(1).Find(&friend)
	if friend.ID == 0 {
		return "您没有添加对方好友"
	}
	return ""
}

func FriendList(list *request.FriendList, userId int) []*dao.Friend {
	var sli []*dao.Friend
	var top []*dao.Friend
	var friend []*dao.Friend
	config.DB.Where("user_id=? and top=1", userId).Order("friend_nick_name ASC").Find(&top)
	config.DB.Where("user_id=? and top=0", userId).Order("friend_nick_name ASC").
		Scopes(Paginate(list.Page, list.PageSize)).Find(&friend)
	for _, d := range top {
		sli = append(sli, d)
	}
	for _, d := range friend {
		sli = append(sli, d)
	}
	return sli
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func Top(top *request.Top, userId int) {
	config.DB.Model(&dao.Friend{}).Where("user_id=? and friend_id=?", userId, top.FriendId).Update("top", 1)
}

func NoTop(top *request.Top, userId int) {
	config.DB.Model(&dao.Friend{}).Where("user_id=? and friend_id=?", userId, top.FriendId).Update("top", 0)
}

func Delete(delete2 *request.Delete, userId int) string {
	err := config.DB.Where("user_id=? and friend_id=?", userId, delete2.FriendId).Delete(&dao.Friend{}).Error
	if err != nil {
		return "删除失败"
	}
	err = config.DB.Where("user_id=? and friend_id=?", delete2.FriendId, userId).Delete(&dao.Friend{}).Error
	if err != nil {
		return "删除失败"
	}
	return ""
}
